//
//  NSString+PinYin.m
//  517TelMeeting
//
//  Created by yunsheng on 15/7/24.
//  Copyright (c) 2015年 517na. All rights reserved.
//

#import "NSString+PinYin.h"

@implementation NSString (PinYin)

-(NSString *)firstCharOfPY
{
    NSString * regex = @"[\u4e00-\u9fa5]";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    NSString *str = [self substringToIndex:1];
    if ([pred evaluateWithObject:str]) {
        NSMutableString *source = [str mutableCopy];
        CFStringTransform((__bridge CFMutableStringRef)source, NULL, kCFStringTransformMandarinLatin, NO);
        CFStringTransform((__bridge CFMutableStringRef)source, NULL, kCFStringTransformStripDiacritics, NO);
        return [[source substringToIndex:1] uppercaseString];
    } else {
        regex = @"[A-Z]";
        pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
        NSString *upperStr = [str uppercaseString];
        if ([pred evaluateWithObject:upperStr]) {
            return upperStr;
        } else {
            return @"#";
        }
    }
}

-(NSString *)QP
{
    NSMutableString *source = [[self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] mutableCopy];
    CFStringTransform((__bridge CFMutableStringRef)source, NULL, kCFStringTransformMandarinLatin, NO);
    CFStringTransform((__bridge CFMutableStringRef)source, NULL, kCFStringTransformStripDiacritics, NO);
    
    return [source uppercaseString];
}

-(NSString *)JPOfPinYin:(NSString *)QPStr
{
    if (QPStr.length == 0) {
        return @"";
    }
    NSArray *array = [QPStr componentsSeparatedByString:@" "];
    NSMutableString *str = [[NSMutableString alloc] init];
    for (int i = 0; i < array.count; i++) {
        [str appendString:[array[i] substringToIndex:1]];
    }
    return [str uppercaseString];
}

@end
